package com.project.zsxd.server.common.bean;

import com.project.zsxd.server.common.enums.ResultEnum;
import com.project.zsxd.server.common.exception.SystemException;


/**
 * 结果类
 * Created by kaler on 2020/3/27.
 */
public class Result {

    /**
     * 结果代码
     */
    private String code;
    /**
     * 结果信息
     */
    private String message;
    /**
     * 是否响应成功 true=成功，false=失败
     */
    private Boolean flag;
    /**
     * 结果数据
     */
    private Object data;

    public Result() {
        this.flag = false;
    }

    public Result(ResultEnum resultEnum, Boolean flag, Object data) {
        this.code = resultEnum.getCode();
        this.message = resultEnum.getMessage();
        this.flag = flag == null ? false : flag;
        this.data = data;
    }

    public Result(SystemException exception) {
        this.code = exception.getErrorCode();
        this.message = exception.getErrorMessage();
        this.flag = false;
    }

    /**
     * 返回成功结果
     * @param data
     * @return
     */
    public static Result success(Object data) {
        Result result = new Result();
        result.setData(data);
        return result;
    }

    /**
     * 返回失败结果
     * @param resultEnum
     * @return
     */
    public static Result fail(ResultEnum resultEnum) {
        Result result = new Result();
        result.setResultEnum(resultEnum);
        return result;
    }

    public String getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public Boolean getFlag() {
        return flag;
    }

    public void setFlag(Boolean flag) {
        this.flag = flag;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.flag = true;
        this.data = data;
    }

    public void setResultEnum(ResultEnum resultEnum) {
        this.code = resultEnum.getCode();
        this.message = resultEnum.getMessage();
    }

    @Override
    public String toString() {
        return "Result{" +
                "code='" + code + '\'' +
                ", message='" + message + '\'' +
                ", flag=" + flag +
                ", data=" + data +
                '}';
    }
}
